package com.yy.permissions_general.module.system.notice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.notice.entity.Notice;
import com.yy.permissions_general.module.system.notice.mapper.NoticeMapper;
import com.yy.permissions_general.module.system.notice.service.NoticeService;
import com.yy.permissions_general.utils.ResultUtil;
import com.yy.permissions_general.utils.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 站内消息前端控制器
 * </p>
 *
 * @author YY
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private NoticeMapper noticeMapper;

    /**
    * 查询列表
    * @param notice
    * @param limit
    * @param page
    * @return
    */
    @MyLog("查询列表")
    @NoRepeatSubmit(key="notice.list")
    @RequestMapping("/list")
    public ResultUtil list(Notice notice,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(noticeService.selectPageList(notice,limit,page));
    }

    /**
     * 消息列表
     * @return
     */
    @MyLog("消息列表")
    @NoRepeatSubmit(key="notice.getNoticeList")
    @RequestMapping("/getNoticeList")
    public ResultUtil getNoticeList() {
        List<Map> result = new ArrayList<>();

        QueryWrapper<Notice> publicParam = new QueryWrapper<>();
        publicParam.eq("type","public");

        QueryWrapper<Notice> privateParam = new QueryWrapper<>();
        privateParam.eq("type","private");
        privateParam.eq("accept_id",SecurityUtil.getCurrentUser().getId());

        QueryWrapper<Notice> noticeParam = new QueryWrapper<>();
        noticeParam.eq("type","notice");

        Map<String, Object> publicArray = new HashMap<>(3);
        publicArray.put("id", "1");
        publicArray.put("title", "公告");
        publicArray.put("children", noticeMapper.selectList(publicParam));

        Map<String, Object> privateArray = new HashMap<>(3);
        privateArray.put("id", "2");
        privateArray.put("title", "私信");
        privateArray.put("children", noticeMapper.selectList(privateParam));

        Map<String, Object> noticeArray = new HashMap<>(3);
        noticeArray.put("id", "3");
        noticeArray.put("title", "通知");
        noticeArray.put("children", noticeMapper.selectList(noticeParam));

        result.add(publicArray);
        result.add(privateArray);
        result.add(noticeArray);
      return ResultUtil.buildPagination(result);
    }

    /**
    * 新增数据
    * @param notice
    * @return
    */
    @MyLog("新增数据")
    @NoRepeatSubmit(key="notice.addNotice")
    @PreAuthorize("hasPermission('/notice/addNotice','notice:addNotice')")
    @RequestMapping("/addNotice")
    public ResultUtil addNotice(Notice notice) {
        noticeService.addNotice(notice);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param notice
    * @return
    */
    @MyLog("修改数据")
    @NoRepeatSubmit(key="notice.editNotice")
    @PreAuthorize("hasPermission('/notice/editNotice','notice:editNotice')")
    @RequestMapping("/editNotice")
    public ResultUtil editNotice(Notice notice) {
        noticeService.editNotice(notice);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @MyLog("查看详情")
    @RequestMapping("/detailNotice")
    public ResultUtil detailNotice(Integer id) {
        Notice notice = noticeService.getDetailsById(id);
        return ResultUtil.success(notice);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @MyLog("删除")
    @NoRepeatSubmit(key="notice.delete")
    @PreAuthorize("hasPermission('/notice/delete','notice:delete')")
    @RequestMapping("/delete")
    public ResultUtil delete(Integer id) {
        noticeService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @MyLog("批量删除")
    @NoRepeatSubmit(key="notice.deleteBatch")
    @PreAuthorize("hasPermission('/notice/deleteBatch','notice:deleteBatch')")
    @RequestMapping("/deleteBatch")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        noticeService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
